﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
namespace syc.Produce
{
    public partial class 月原料采购计划录入 : System.Web.UI.Page
    {
        protected string 年度;
        protected string 季度;
        protected string 月份;

        protected double 油管计划量;
        protected double 套管计划量;        
        protected double 油管接箍计划量;
        protected double 套管接箍计划量;
        protected double 成品套管接箍计划量;

        protected double 油管N80计划量;
        protected double 套管N80计划量;
        protected double 油管N80接箍计划量;
        protected double 套管N80接箍计划量;
        protected double 成品套管N80接箍计划量;

        protected double 油管单价;
        protected double 套管单价;       
        protected double 油管接箍单价;
        protected double 套管接箍单价;
        protected double 成品套管接箍单价;

        protected double 油管N80单价;
        protected double 套管N80单价;
        protected double 油管N80接箍单价;
        protected double 套管N80接箍单价;
        protected double 成品套管N80接箍单价;

        protected double 油管金额;
        protected double 套管金额;       
        protected double 油管接箍金额;
        protected double 套管接箍金额;
        protected double 成品套管接箍金额;

        protected double 油管N80金额;
        protected double 套管N80金额;
        protected double 油管N80接箍金额;
        protected double 套管N80接箍金额;
        protected double 成品套管N80接箍金额;

        double[] chanliang = new double[10];
        double[] chanliang1 = new double[10];
        protected HSSFWorkbook hssfworkbook;
        protected string y;
        protected string m;
        protected void Page_Load(object sender, EventArgs e)
        {  
            y=Request.QueryString["y"];
            m=Request.QueryString["m"];
           
            if (!this.IsPostBack)
            {

                年度 = this.NianDu.Text;
                this.NianDu.Text = DateTime.Now.Year.ToString();
                this.YueFen.SelectedValue = DateTime.Today.Month.ToString();             

                if (y != null&&m != null)
                {
                    月份 = m;
                    this.YueFen.SelectedValue = 月份;
                    this.NianDu.Text = y;

                    this.NianDu.Enabled = false;
                    this.YueFen.Enabled = false;
                    GetData1(this.NianDu.Text, this.YueFen.Text);
                    this.YouGuan.Text = chanliang[0].ToString();
                    this.TaoGuan.Text = chanliang[1].ToString();
                    this.YouGuanJieGu.Text = chanliang[2].ToString();
                    this.TaoGuanJieGu.Text = chanliang[3].ToString();
                    this.ChengPinJieGu.Text = chanliang[4].ToString();

                    this.YouGuanJiHuaZhiJinDanJia.Text = chanliang[5].ToString();
                    this.TaoGuanJiHuaZhiJinDanJia.Text = chanliang[6].ToString();
                    this.YouGuanJieGuJiHuaZhiJinDanJia.Text = chanliang[7].ToString();
                    this.TaoGuanJieGuJiHuaZhiJinDanJia.Text = chanliang[8].ToString();
                    this.ChengPinJieGuJiHuaZhiJinDanJia.Text = chanliang[9].ToString();

                    this.YouGuan_N80.Text = chanliang1[0].ToString();
                    this.TaoGuan_N80.Text = chanliang1[1].ToString();
                    this.YouGuanJieGu_N80.Text = chanliang1[2].ToString();
                    this.TaoGuanJieGu_N80.Text = chanliang1[3].ToString();
                    this.ChengPinJieGu_N80.Text = chanliang1[4].ToString();                    

                    this.YouGuanJiHuaZhiJinDanJia_N80.Text = chanliang1[5].ToString();
                    this.TaoGuanJiHuaZhiJinDanJia_N80.Text = chanliang1[6].ToString();
                    this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text = chanliang1[7].ToString();
                    this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text = chanliang1[8].ToString();
                    this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text = chanliang1[9].ToString();

                  
                    Button_Calc_Click(null, null);
                }
                else
                {
                    月份 = this.YueFen.SelectedValue;
                }
            }
            if (this.YueFen.SelectedValue == "1" || this.YueFen.SelectedValue == "2" || this.YueFen.SelectedValue == "3")
            {
                季度 = "1季度";
                this.JiDu.SelectedValue = "13";
            }
            if (this.YueFen.SelectedValue == "4" || 月份 == "5" || this.YueFen.SelectedValue == "6")
            {
                季度 = "2季度";
                this.JiDu.SelectedValue = "14";
            }
            if (this.YueFen.SelectedValue == "7" || 月份 == "8" || this.YueFen.SelectedValue == "9")
            {
                季度 = "3季度";
                this.JiDu.SelectedValue = "15";
            }
            if (this.YueFen.SelectedValue == "10" || 月份 == "11" || this.YueFen.SelectedValue == "12")
            {
                季度 = "4季度";
                this.JiDu.SelectedValue = "16";
            }
            this.JiDu.SelectedItem.Text = 季度;
            GetData(this.NianDu.Text, this.JiDu.Text);
           
            
           
        }
        protected void GetData(string year,string jidu)
        {
            this.Message.Text = "·准备就绪";
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select * from 原料采购计划 where 年度='" + year + "' and 季度='" + jidu + "'", Conn);
            sda.Fill(ds);
            try
            {
                油管计划量 = double.Parse(ds.Tables[0].Rows[0][2].ToString());
                油管单价 = double.Parse(ds.Tables[0].Rows[0][3].ToString());
                油管金额 = 油管计划量 * 油管单价;

                套管计划量 = double.Parse(ds.Tables[0].Rows[0][4].ToString());
                套管单价 = double.Parse(ds.Tables[0].Rows[0][5].ToString());
                套管金额 = 套管计划量 * 套管单价;

                油管接箍计划量 = double.Parse(ds.Tables[0].Rows[0][6].ToString());
                油管接箍单价 = double.Parse(ds.Tables[0].Rows[0][7].ToString());
                油管接箍金额 = 油管接箍计划量 * 油管接箍单价;

                套管接箍计划量 = double.Parse(ds.Tables[0].Rows[0][8].ToString());
                套管接箍单价 = double.Parse(ds.Tables[0].Rows[0][9].ToString());
                套管接箍金额 = 套管接箍计划量 * 套管接箍单价;

                成品套管接箍计划量 = double.Parse(ds.Tables[0].Rows[0][10].ToString());
                成品套管接箍单价 = double.Parse(ds.Tables[0].Rows[0][11].ToString());
                成品套管接箍金额 = 成品套管接箍计划量 * 成品套管接箍单价;

                油管N80计划量 = double.Parse(ds.Tables[0].Rows[0][12].ToString());
                油管N80单价 = double.Parse(ds.Tables[0].Rows[0][13].ToString());
                油管N80金额 = 油管N80计划量 * 油管N80单价;

                套管N80计划量 = double.Parse(ds.Tables[0].Rows[0][14].ToString());
                套管N80单价 = double.Parse(ds.Tables[0].Rows[0][15].ToString());
                套管N80金额 = 套管N80计划量 * 套管N80单价;

                油管N80接箍计划量 = double.Parse(ds.Tables[0].Rows[0][16].ToString());
                油管N80接箍单价 = double.Parse(ds.Tables[0].Rows[0][17].ToString());
                油管N80接箍金额 = 油管N80接箍计划量 * 油管N80接箍单价;

                套管N80接箍计划量 = double.Parse(ds.Tables[0].Rows[0][18].ToString());
                套管N80接箍单价 = double.Parse(ds.Tables[0].Rows[0][19].ToString());
                套管N80接箍金额 = 套管N80接箍计划量 * 套管N80接箍单价;

                成品套管N80接箍计划量 = double.Parse(ds.Tables[0].Rows[0][20].ToString());
                成品套管N80接箍单价 = double.Parse(ds.Tables[0].Rows[0][21].ToString());
                成品套管N80接箍金额 = 成品套管N80接箍计划量 * 成品套管N80接箍单价;
            }
            catch
            {
                油管计划量 =0;
                油管单价 =0;
                油管金额 = 0;
                套管计划量 = 0;
                套管单价 = 0;
                套管金额 = 0;
                油管接箍计划量 = 0;
                油管接箍单价 =0;
                油管接箍金额 = 0;
                套管接箍计划量 = 0;
                套管接箍单价 = 0;
                套管接箍金额 = 0;
                成品套管接箍计划量 = 0;
                成品套管接箍单价 = 0;
                成品套管接箍金额 =0;
                油管N80计划量 = 0;
                油管N80单价 = 0;
                油管N80金额 = 0;
                套管N80计划量 = 0;
                套管N80单价 = 0;
                套管N80金额 = 0;
                油管N80接箍计划量 = 0;
                油管N80接箍单价 = 0;
                油管N80接箍金额 = 0;
                套管N80接箍计划量 = 0;
                套管N80接箍单价 = 0;
                套管N80接箍金额 = 0;
                成品套管N80接箍计划量 = 0;
                成品套管N80接箍单价 = 0;
                成品套管N80接箍金额 = 0;
                this.Message.Text = ".供应处暂时还没有制订季度计划!";
            }

        }
        protected void GetData1(string year, string month)
        {
            this.Message.Text = "·准备就绪";
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = @"select 计划购进量,单价,金额 from 生产处月原料采购计划 where 年度='" + year + "' and 月份='" + month + "' and 原料ID between 1 and 6 ";
            SqlDataReader Reader = Cmd.ExecuteReader();
            int i = 0;
            while (Reader.Read())
            {
                //油管量
                chanliang[i] = Reader.GetDouble(0);
                chanliang[i+5] = double.Parse(Reader.GetDecimal(1).ToString());
                i++;
            }
            Reader.Close();
            Cmd.CommandText = @"select 计划购进量,单价,金额 from 生产处月原料采购计划 where 年度='" + year + "' and 月份='" + month + "' and 原料ID between 101 and 106";
            Reader = Cmd.ExecuteReader();
            int j = 0;
            while (Reader.Read())
            {
                //油管量
                chanliang1[j] = Reader.GetDouble(0);
                chanliang1[j + 5] = double.Parse(Reader.GetDecimal(1).ToString());
                j++;
            }
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
        }


        protected void NianDu_SelectedIndexChanged(object sender, EventArgs e)
        {
            YueFen_SelectedIndexChanged(null, null);
        }

        protected void YueFen_SelectedIndexChanged(object sender, EventArgs e)
        {
            string 月份 = this.YueFen.SelectedValue;
            if (月份 == "1" || 月份 == "2" || 月份 == "3")
            {
                季度 = "1季度";
                this.JiDu.SelectedValue = "13";
            }
            if (月份 == "4" || 月份 == "5" || 月份 == "6")
            {
                季度 = "2季度";
                this.JiDu.SelectedValue = "14";
            }
            if (月份 == "7" || 月份 == "8" || 月份 == "9")
            {
                季度 = "3季度";
                this.JiDu.SelectedValue = "15";
            }
            if (月份 == "10" || 月份 == "11" || 月份 == "12")
            {
                季度 = "4季度";
                this.JiDu.SelectedValue = "16";
            }
            GetData(this.NianDu.Text, this.JiDu.Text);
        }
        private bool CheckInput()
        {
            if (!double.TryParse(this.YouGuan.Text, out chanliang[0]))
            {
                this.Message.Text = "·原料油管 计划购进量录入不正确";
                return false;
            }
            else
            if (chanliang[0] < 0)
            {
                    this.Message.Text = "·原料油管 计划购进量不能小于0";
                    return false;
            }
            if (!double.TryParse(this.TaoGuan.Text, out chanliang[1]))
            {
                this.Message.Text = "·原料套管 计划购进量录入不正确";
                return false;
            }
            else
                if (chanliang[1] < 0)
                {
                    this.Message.Text = "·原料套管 计划购进量不能小于0";
                    return false;
                }
            if (!double.TryParse(this.YouGuanJieGu.Text, out chanliang[2]))
            {
                this.Message.Text = "·原料油管接箍 计划购进量录入不正确";
                return false;
            }
            else
                if (chanliang[2] < 0)
                {
                    this.Message.Text = "·原料油管接箍 计划购进量不能小于0";
                    return false;
                }
            if (!double.TryParse(this.TaoGuanJieGu.Text, out chanliang[3]))
            {
                this.Message.Text = "·原料套管接箍 计划购进量录入不正确";
                return false;
            }
            else
                if (chanliang[3] < 0)
                {
                    this.Message.Text = "·原料套管接箍 计划购进量不能小于0";
                    return false;
                }
            if (!double.TryParse(this.ChengPinJieGu.Text, out chanliang[4]))
            {
                this.Message.Text = "·原料成品套管接箍 计划购进量录入不正确";
                return false;
            }
            else
                if (chanliang[4] < 0)
                {
                    this.Message.Text = "·原料成品套管接箍 计划购进量不能小于0";
                    return false;
                }
            if (!double.TryParse(this.YouGuanJiHuaZhiJinDanJia.Text, out chanliang[5]))
            {
                this.Message.Text = "·原料油管 油管单价录入不正确";
                return false;
            }
            else
                if (chanliang[5] < 0)
                {
                    this.Message.Text = "·原料油管 油管单价不能小于0";
                    return false;
                }
            if (!double.TryParse(this.TaoGuanJiHuaZhiJinDanJia.Text, out chanliang[6]))
            {
                this.Message.Text = "·原料套管 套管单价录入不正确";
                return false;
            }
            else
                if (chanliang[6] < 0)
                {
                    this.Message.Text = "·原料套管 套管单价不能小于0";
                    return false;
                }
            if (!double.TryParse(this.YouGuanJieGuJiHuaZhiJinDanJia.Text, out chanliang[7]))
            {
                this.Message.Text = "·原料油管接箍 油管接箍单价录入不正确";
                return false;
            }
            else
                if (chanliang[7] < 0)
                {
                    this.Message.Text = "·原料油管接箍 油管接箍单价不能小于0";
                    return false;
                }
            if (!double.TryParse(this.TaoGuanJieGuJiHuaZhiJinDanJia.Text, out chanliang[8]))
            {
                this.Message.Text = "·原料套管接箍 套管接箍单价录入不正确";
                return false;
            }
            else
                if (chanliang[8] < 0)
                {
                    this.Message.Text = "·原料套管接箍 套管接箍单价不能小于0";
                    return false;
                }
            if (!double.TryParse(this.ChengPinJieGuJiHuaZhiJinDanJia.Text, out chanliang[9]))
            {
                this.Message.Text = "·原料成品套管接箍 成品套管接箍单价录入不正确";
                return false;
            }
            else
                if (chanliang[9] < 0)
                {
                    this.Message.Text = "·原料成品套管接箍 成品套管接箍单价不能小于0";
                    return false;
                }
            if (!double.TryParse(this.YouGuan_N80.Text, out chanliang1[0]))
            {
                this.Message.Text = "·原料油管(N80) 计划购进量录入不正确";
                return false;
            }
            else
                if (chanliang1[0] < 0)
                {
                    this.Message.Text = "·原料油管(N80) 计划购进量不能小于0";
                    return false;
                }
            if (!double.TryParse(this.TaoGuan_N80.Text, out chanliang1[1]))
            {
                this.Message.Text = "·原料套管(N80) 计划购进量录入不正确";
                return false;
            }
            else
                if (chanliang1[1] < 0)
                {
                    this.Message.Text = "·原料套管(N80) 计划购进量不能小于0";
                    return false;
                }
            if (!double.TryParse(this.YouGuanJieGu_N80.Text, out chanliang1[2]))
            {
                this.Message.Text = "·原料油管(N80)接箍 计划购进量录入不正确";
                return false;
            }
            else
                if (chanliang1[2] < 0)
                {
                    this.Message.Text = "·原料油管(N80)接箍 计划购进量不能小于0";
                    return false;
                }
            if (!double.TryParse(this.TaoGuanJieGu_N80.Text, out chanliang1[3]))
            {
                this.Message.Text = "·原料套管(N80)接箍 计划购进量录入不正确";
                return false;
            }
            else
                if (chanliang1[3] < 0)
                {
                    this.Message.Text = "·原料套管(N80)接箍 计划购进量不能小于0";
                    return false;
                }
            if (!double.TryParse(this.ChengPinJieGu_N80.Text, out chanliang1[4]))
            {
                this.Message.Text = "·原料成品套管(N80)接箍 计划购进量录入不正确";
                return false;
            }
            else
                if (chanliang1[4] < 0)
                {
                    this.Message.Text = "·原料成品套管(N80)接箍 计划购进量不能小于0";
                    return false;
                }
            if (!double.TryParse(this.YouGuanJiHuaZhiJinDanJia_N80.Text, out chanliang1[5]))
            {
                this.Message.Text = "·原料油管(N80) 油管单价录入不正确";
                return false;
            }
            else
                if (chanliang1[5] < 0)
                {
                    this.Message.Text = "·原料油管(N80) 油管单价不能小于0";
                    return false;
                }
            if (!double.TryParse(this.TaoGuanJiHuaZhiJinDanJia_N80.Text, out chanliang1[6]))
            {
                this.Message.Text = "·原料套管(N80) 套管单价录入不正确";
                return false;
            }
            else
                if (chanliang1[6] < 0)
                {
                    this.Message.Text = "·原料套管(N80) 套管单价不能小于0";
                    return false;
                }
            if (!double.TryParse(this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text, out chanliang1[7]))
            {
                this.Message.Text = "·原料油管(N80)接箍 油管接箍单价录入不正确";
                return false;
            }
            else
                if (chanliang1[7] < 0)
                {
                    this.Message.Text = "·原料油管(N80)接箍 油管接箍单价不能小于0";
                    return false;
                }
            if (!double.TryParse(this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text, out chanliang1[8]))
            {
                this.Message.Text = "·原料套管(N80)接箍 套管接箍单价录入不正确";
                return false;
            }
            else
                if (chanliang1[8] < 0)
                {
                    this.Message.Text = "·原料套管(N80)接箍 套管接箍单价不能小于0";
                    return false;
                }
            if (!double.TryParse(this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text, out chanliang1[9]))
            {
                this.Message.Text = "·原料成品套管(N80)接箍 成品套管接箍单价录入不正确";
                return false;
            }
            else
                if (chanliang1[9] < 0)
                {
                    this.Message.Text = "·原料成品套管(N80)接箍 成品套管接箍单价不能小于0";
                    return false;
                }

       
            return true;
        }

        protected void InitializeWorkbook()
        {

            FileStream file = new FileStream(HttpContext.Current.Server.MapPath("/Sale/模板/原料采购计划.xls"), FileMode.Open, FileAccess.Read);

            hssfworkbook = new HSSFWorkbook(file);

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "陕西延长石油材料有限公司";
            hssfworkbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "报表";
            si.Title = "原料采购计划";
            hssfworkbook.SummaryInformation = si;
        }
        protected void WriteToFile()
        {
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode("原料采购计划") + ".xls\"");
            hssfworkbook.Write(Response.OutputStream);
        }
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (!CheckInput())
            {
                return;
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            try
            {
                if (this.NianDu.Enabled)
                {

                    Cmd.CommandText =
    "if EXISTS(select * from 生产处月原料采购计划 where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + ")\n"
    + "begin\n"
    + "return\n"
    + "end\n"
    + "else\n"
    + "begin\n"
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",1," + double.Parse(this.YouGuan.Text) + "," + this.YouGuanJiHuaZhiJinDanJia.Text + "," + (double.Parse(this.YouGuanJiHuaZhiJinDanJia.Text) * (double.Parse(this.YouGuan.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n"
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",2," + double.Parse(this.TaoGuan.Text) + "," + this.TaoGuanJiHuaZhiJinDanJia.Text + "," + (double.Parse(this.TaoGuanJiHuaZhiJinDanJia.Text) * (double.Parse(this.TaoGuan.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n"
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",3," + double.Parse(this.YouGuanJieGu.Text) + "," + this.YouGuanJieGuJiHuaZhiJinDanJia.Text + "," + (double.Parse(this.YouGuanJieGuJiHuaZhiJinDanJia.Text) * (double.Parse(this.YouGuanJieGu.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n"
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",4," + double.Parse(this.TaoGuanJieGu.Text) + "," + this.TaoGuanJieGuJiHuaZhiJinDanJia.Text + "," + (double.Parse(this.TaoGuanJieGuJiHuaZhiJinDanJia.Text) * (double.Parse(this.TaoGuanJieGu.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n"
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",6," + double.Parse(this.ChengPinJieGu.Text) + "," + this.ChengPinJieGuJiHuaZhiJinDanJia.Text + "," + (double.Parse(this.ChengPinJieGuJiHuaZhiJinDanJia.Text) * (double.Parse(this.ChengPinJieGu.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n" 
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",101," + double.Parse(this.YouGuan_N80.Text) + "," + this.YouGuanJiHuaZhiJinDanJia_N80.Text + "," + (double.Parse(this.YouGuanJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.YouGuan_N80.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n"
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",102," + double.Parse(this.TaoGuan_N80.Text) + "," + this.TaoGuanJiHuaZhiJinDanJia_N80.Text + "," + (double.Parse(this.TaoGuanJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.TaoGuan_N80.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n"
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",103," + double.Parse(this.YouGuanJieGu_N80.Text) + "," + this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text + "," + (double.Parse(this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.YouGuanJieGu_N80.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n"
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",104," + double.Parse(this.TaoGuanJieGu_N80.Text) + "," + this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text + "," + (double.Parse(this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.TaoGuanJieGu_N80.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n"
    + "insert into 生产处月原料采购计划(年度,月份,原料ID,计划购进量,单价,金额,日期)"
    + "values(" + this.NianDu.Text + "," + this.YueFen.Text + ",106," + double.Parse(this.ChengPinJieGu_N80.Text) + "," + this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text + "," + (double.Parse(this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.ChengPinJieGu_N80.Text)) + ",'" + DateTime.Now.ToString("yyyy-MM-dd") + "'")
    + ")\n" 
    + "end";
                }
                else
                {
                    Cmd.CommandText =                  
                     "update 生产处月原料采购计划 set 计划购进量=" + chanliang[0]+",单价=" + chanliang[5]+",金额=" + (chanliang[0] * chanliang[5])+" where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text+" and 原料ID=1 \n"
                    + "update 生产处月原料采购计划 set 计划购进量=" + chanliang[1] + ",单价=" + chanliang[6] + ",金额=" + (chanliang[1] * chanliang[6]) + " where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 原料ID=2 \n"
                    + "update 生产处月原料采购计划 set 计划购进量=" + chanliang[2] + ",单价=" + chanliang[7] + ",金额=" + (chanliang[2] * chanliang[7]) + " where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 原料ID=3 \n"
                    + "update 生产处月原料采购计划 set 计划购进量=" + chanliang[3] + ",单价=" + chanliang[8] + ",金额=" + (chanliang[3] * chanliang[8]) + " where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 原料ID=4 \n"
                    + "update 生产处月原料采购计划 set 计划购进量=" + chanliang[4] + ",单价=" + chanliang[9] + ",金额=" + (chanliang[4] * chanliang[9]) + " where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 原料ID=6 \n"
                    +" update 生产处月原料采购计划 set 计划购进量=" + chanliang1[0] + ",单价=" + chanliang1[5] + ",金额=" + (chanliang1[0] * chanliang1[5]) + " where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 原料ID=101 \n"
                    + "update 生产处月原料采购计划 set 计划购进量=" + chanliang1[1] + ",单价=" + chanliang1[6] + ",金额=" + (chanliang1[1] * chanliang1[6]) + " where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 原料ID=102 \n"
                    + "update 生产处月原料采购计划 set 计划购进量=" + chanliang1[2] + ",单价=" + chanliang1[7] + ",金额=" + (chanliang1[2] * chanliang1[7]) + " where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 原料ID=103 \n"
                    + "update 生产处月原料采购计划 set 计划购进量=" + chanliang1[3] + ",单价=" + chanliang1[8] + ",金额=" + (chanliang1[3] * chanliang1[8]) + " where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 原料ID=104 \n"
                    + "update 生产处月原料采购计划 set 计划购进量=" + chanliang1[4] + ",单价=" + chanliang1[9] + ",金额=" + (chanliang1[4] * chanliang1[9]) + " where 年度=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + " and 原料ID=106 \n";  
                   
                }


                if (Cmd.ExecuteNonQuery() < 1)
                {
                    if (this.NianDu.Enabled)
                    {
                        this.Message.Text = "·保存失败，计划已经存在";
                    }
                    else
                    {
                        this.Message.Text = "·保存失败，您正在修改的计划可能已经被删除";
                    }
                }
                else
                {
                    this.Message.Text = "·保存成功";
                    this.YouGuan.Enabled = false;
                    this.YouGuan.CssClass = "blank_center";
                    this.YouGuanJiHuaZhiJinDanJia.Enabled = false;
                    this.YouGuanJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.TaoGuan.Enabled = false;
                    this.TaoGuan.CssClass = "blank_center";
                    this.TaoGuanJiHuaZhiJinDanJia.Enabled = false;
                    this.TaoGuanJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.YouGuanJieGu.Enabled = false;
                    this.YouGuanJieGu.CssClass = "blank_center";
                    this.YouGuanJieGuJiHuaZhiJinDanJia.Enabled = false;
                    this.YouGuanJieGuJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.TaoGuanJieGu.Enabled = false;
                    this.TaoGuanJieGu.CssClass = "blank_center";
                    this.TaoGuanJieGuJiHuaZhiJinDanJia.Enabled = false;
                    this.TaoGuanJieGuJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.ChengPinJieGu.Enabled = false;
                    this.ChengPinJieGu.CssClass = "blank_center";
                    this.ChengPinJieGuJiHuaZhiJinDanJia.Enabled = false;
                    this.ChengPinJieGuJiHuaZhiJinDanJia.CssClass = "blank_center";

                    this.YouGuan_N80.Enabled = false;
                    this.YouGuan_N80.CssClass = "blank_center";
                    this.YouGuanJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.YouGuanJiHuaZhiJinDanJia_N80.CssClass = "blank_center";

                    this.TaoGuan_N80.Enabled = false;
                    this.TaoGuan_N80.CssClass = "blank_center";
                    this.TaoGuanJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.TaoGuanJiHuaZhiJinDanJia_N80.CssClass = "blank_center";

                    this.YouGuanJieGu_N80.Enabled = false;
                    this.YouGuanJieGu_N80.CssClass = "blank_center";
                    this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.YouGuanJieGuJiHuaZhiJinDanJia_N80.CssClass = "blank_center";

                    this.TaoGuanJieGu_N80.Enabled = false;
                    this.TaoGuanJieGu_N80.CssClass = "blank_center";
                    this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.CssClass = "blank_center";

                    this.ChengPinJieGu_N80.Enabled = false;
                    this.ChengPinJieGu_N80.CssClass = "blank_center";
                    this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Enabled = false;
                    this.ChengPinJieGuJiHuaZhiJinDanJia_N80.CssClass = "blank_center";
                    this.NianDu.Enabled = false;
                    this.YueFen.Enabled = false;
                    Button_Calc_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                this.Message.Text = "·保存失败，计划已经存在";
            }

            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();
        }

        protected void Button_Calc_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                this.YouGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJiHuaZhiJinDanJia.Text) * (double.Parse(this.YouGuan.Text))));
                this.TaoGuanJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJiHuaZhiJinDanJia.Text) * (double.Parse(this.TaoGuan.Text))));
                this.YouGuanJieGuJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJieGuJiHuaZhiJinDanJia.Text) * (double.Parse(this.YouGuanJieGu.Text))));
                this.TaoGuanJieGuJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJieGuJiHuaZhiJinDanJia.Text) * (double.Parse(this.TaoGuanJieGu.Text))));
                this.ChengPinJieGuJiHuaZhiJinJine.Text = string.Format("{0:0.00}", (double.Parse(this.ChengPinJieGuJiHuaZhiJinDanJia.Text) * (double.Parse(this.ChengPinJieGu.Text))));
                this.YouGuanJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.YouGuan_N80.Text))));
                this.TaoGuanJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.TaoGuan_N80.Text))));
                this.YouGuanJieGuJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.YouGuanJieGuJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.YouGuanJieGu_N80.Text))));
                this.TaoGuanJieGuJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.TaoGuanJieGu_N80.Text))));
                this.ChengPinJieGuJiHuaZhiJinJine_N80.Text = string.Format("{0:0.00}", (double.Parse(this.ChengPinJieGuJiHuaZhiJinDanJia_N80.Text) * (double.Parse(this.ChengPinJieGu_N80.Text))));
            }
        }

        protected void Button_Export_Click(object sender, EventArgs e)
        {
            InitializeWorkbook();
            HSSFSheet sheet1 = hssfworkbook.GetSheet("原料采购计划");
            string title = "";          
            title = this.NianDu.SelectedItem.Text + "年" + this.YueFen.SelectedItem.Text+"月份";         

            sheet1.GetRow(0).GetCell(0).SetCellValue("生产处"+title + "原料采购计划");
            string 时间 = DateTime.Now.ToString("yyyy-MM-dd");
            sheet1.GetRow(1).GetCell(2).SetCellValue("时间:" + 时间);

            sheet1.GetRow(3).GetCell(5).SetCellValue(YouGuan.Text);
            sheet1.GetRow(3).GetCell(7).SetCellValue(YouGuanJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(3).GetCell(9).SetCellValue(YouGuanJiHuaZhiJinJine.Text);

            sheet1.GetRow(4).GetCell(5).SetCellValue(TaoGuan.Text);
            sheet1.GetRow(4).GetCell(7).SetCellValue(TaoGuanJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(4).GetCell(9).SetCellValue(TaoGuanJiHuaZhiJinJine.Text);

            sheet1.GetRow(5).GetCell(5).SetCellValue(YouGuanJieGu.Text);
            sheet1.GetRow(5).GetCell(7).SetCellValue(YouGuanJieGuJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(5).GetCell(9).SetCellValue(YouGuanJieGuJiHuaZhiJinJine.Text);

            sheet1.GetRow(6).GetCell(5).SetCellValue(TaoGuanJieGu.Text);
            sheet1.GetRow(6).GetCell(7).SetCellValue(TaoGuanJieGuJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(6).GetCell(9).SetCellValue(TaoGuanJieGuJiHuaZhiJinJine.Text);

            sheet1.GetRow(7).GetCell(5).SetCellValue(ChengPinJieGu.Text);
            sheet1.GetRow(7).GetCell(7).SetCellValue(ChengPinJieGuJiHuaZhiJinDanJia.Text);
            sheet1.GetRow(7).GetCell(9).SetCellValue(ChengPinJieGuJiHuaZhiJinJine.Text);

            sheet1.GetRow(8).GetCell(5).SetCellValue(YouGuan_N80.Text);
            sheet1.GetRow(8).GetCell(7).SetCellValue(YouGuanJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(8).GetCell(9).SetCellValue(YouGuanJiHuaZhiJinJine_N80.Text);           

            sheet1.GetRow(9).GetCell(5).SetCellValue(TaoGuan_N80.Text);
            sheet1.GetRow(9).GetCell(7).SetCellValue(TaoGuanJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(9).GetCell(9).SetCellValue(TaoGuanJiHuaZhiJinJine_N80.Text);

            sheet1.GetRow(10).GetCell(5).SetCellValue(YouGuanJieGu_N80.Text);
            sheet1.GetRow(10).GetCell(7).SetCellValue(YouGuanJieGuJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(10).GetCell(9).SetCellValue(YouGuanJieGuJiHuaZhiJinJine_N80.Text);

            sheet1.GetRow(11).GetCell(5).SetCellValue(TaoGuanJieGu_N80.Text);
            sheet1.GetRow(11).GetCell(7).SetCellValue(TaoGuanJieGuJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(11).GetCell(9).SetCellValue(TaoGuanJieGuJiHuaZhiJinJine_N80.Text);

            sheet1.GetRow(12).GetCell(5).SetCellValue(ChengPinJieGu_N80.Text);
            sheet1.GetRow(12).GetCell(7).SetCellValue(ChengPinJieGuJiHuaZhiJinDanJia_N80.Text);
            sheet1.GetRow(12).GetCell(9).SetCellValue(ChengPinJieGuJiHuaZhiJinJine_N80.Text);

            WriteToFile();
            Response.End();
        }

       
       

       
    }
}
